// pages/myPics/myPics.js
const app = getApp();
const db = wx.cloud.database();
const httpUrl = "https://6875-huazai-6g820orq10216ac7-1316354189.tcb.qcloud.la/"
var picsArr = [];
var contextColor = {
    "愉悦": "rgba(250, 226, 192, 1);",
    "轻松": "rgba(253, 243, 192, 1);",
    "无语": "rgba(213, 236, 208, 1);",
    "发愁": "rgba(217, 217, 240, 1);",
    "崩溃": "rgba(188, 201, 207, 1);",
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        contextColor: contextColor
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        picsArr = [];
        this.getPics();
    },
    getPics(){
        let that = this;
        db.collection("diaryDB").where({
            _openid: app.globalData.openid,
            cloudPath: db.command.neq({})
        }).orderBy("createTime","desc").get({
            success: function(res) {
                for ( let i in res.data ) {
                    let pics = res.data[i].cloudPath;
                    let diary = res.data[i];
                    for ( let j in pics ) {
                        let id = diary._id;
                        let month = diary.month;
                        let day = diary.day;
                        let text = diary.diary;
                        let hot = diary.hot;
                        let like = Object.keys(diary.like).length;
                        let favourite = Object.keys(diary.favourite).length;
                        let comment = diary.comment.length;
                        let emoji = diary.emoji;
                        let cloudPath = diary.cloudPath;
                        let images = [];
                        for ( let k in cloudPath ) {
                            let url = cloudPath[k];
                            let image = httpUrl + url.substr(72,url.length);
                            images.push(image);
                        }
                        let timeString = diary.createTime.valueOf();
                        let data = {id:id,month:month,day:day,text:text,pic:pics[j],hot:hot,like:like,favourite:favourite,comment:comment,emoji:emoji,diary:text,images:images,cloudPath:cloudPath,timeString:timeString};
                        picsArr.push(data);
                    }
                }
                that.setData({picsArr})
            }
        })
    },
    gotoDiary(e){
        let id = e.currentTarget.dataset.id;
        let year = e.currentTarget.dataset.year;
        let month = e.currentTarget.dataset.month;
        let day = e.currentTarget.dataset.day;
        let emoji = e.currentTarget.dataset.emoji;
        let diary = e.currentTarget.dataset.diary;
        let images = e.currentTarget.dataset.images;
        let cloudPath = e.currentTarget.dataset.cloudimage;
        let timeString = e.currentTarget.dataset.timestring;
        db.collection("diaryDB").doc(id).update({
            data: {
                hot: db.command.inc(1)
            }
        })
        wx.navigateTo({
            url: '../diary/diary',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{year:year,month:month,authorid:app.globalData.openid,day:day,emoji:emoji,diary:diary,id:id,images:images,cloudimage:cloudPath,publish:false,fixmode: false,visit:true,timeString:timeString});
            }
        })
    },
    lookPic(e){
        console.log(e)
        let urls = [];
        urls.push(e.currentTarget.dataset.pic);
        wx.previewImage({
            urls: urls,
            showmenu: true
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})